var events = require('events');
var Emitter = events.EventEmitter;

function Dog(name, energy) {
    Emitter.call(this);
    this.name = name;
    this.energy = energy;
    /*function函数，函数内this指向取决于调用主体
    箭头函数，函数内this指向函数的上下文 
     */
    var intervalId = setInterval(() => {
        if (this.energy >= 0) {
            this.emit('bark');
            this.energy--;
        } else {
            clearInterval(intervalId);
        }
    }, 1000)
}
for (var i in Emitter.prototype) {
    Dog.prototype[i] = Emitter.prototype[i];
}
module.exports = Dog;